::::::::::::::::::::::::::::::::::::::::::::::::::::
The purpose of this program to change the names of all the files in a selected directory, with a selected .EXT, to any chosen name followed by a number allocated consecutively. At first glance some might wonder why on earth anyone would need to adopt the possibly dangerous procedure of changing the filenames of a number of files?
I can give one very good example. Programs that rip numerous tracks from a Music CD, by converting them into MP3 or say WMA files, usually give all the new files the same name such as Audio1, Audio2 or Track1 and Track2 etc. Therefore if you are forgetful and rip more files from another CD into the same Directory, the software will quite probably overwrite those existing files with the same number, and perhaps without warning you! Bearing in mind many CDs last no longer than LP records, of around 40 Minutes, you like me will often be tempted to burn two LPs or CDs onto one 80 minute blank. As a result the aforementioned problem might occur; hence the need to give the first batch a new name such as that of the Artist.
A few notes on the code might be helpful. Firstly the opening window emphasises renaming will not occur unless confirmed with the CONTINUE menu. Also a warning it could prove useful to create a printout or the changes just in case you are careless enough to rename files by mistake. Quite naturally if you wish to first test the program safely, I strongly suggest you try it out on a number of files copied into a Temporary Directory. I expect you like me run at least one TEMP directory from your Root Directory.
Those following my previous offerings to the News Letters will recognise my trick of changing the initial search directory to the Root. Also the way the DIR.BAS supplied with LB is adapted to select files by the .EXT of the any chosen file. Note the use of the [tbox] provided by someone some time back to eliminate screen flicker with TEXT windows, and also the text screen dump in [sdump]. You will notice the final printout line is found by lpos but set back 2 lines to avoid the captions.
As always comments are always welcome in the Conforum.
--- Gordon Sweet
' Usefull with CD ripping to rename all the tracks,
' as some burners automatically sort files alphabetical.
' Confirmation needed before any Rename is effected.
nomainwin
texteditor #r.tb 20,20,0,0 'this is invisible
menu #r, "&CONTINUE","&EFFECT RENAME",[rename],_
"&ABORT RENAME",[pick],&PRINTOUT,[sdump],|,"&Quit",[quit]
open "Renaming Multiple Files." for text_fs as #r
dim dir$(10,3)
DefaultDir$ = left$(DefaultDir$,2)+"\" ' Set to Root Dir.
[pick]
redim dir$(10,3)
#r "!trapclose [quit]" : #r "!cls" : #r "!font arial 16 bold"
#r space$(45);"Mutiple File Renamer" : #r " "
#r " Enter any New filename, and the number to be given to the first file"
#r " the remainder will be allocated the numbers following consequetively."
#r " Next Select the Directory, and type of file by .EXT. If you do not want"
#r " an Alpha. sort leave it as 0. You must confirm before renaming occurs"
[box2] if bx=1 then close #b
tx1$="Leave Name Blank to retain existing filenames"
tx2$="Any New Filename for All"
tx3$="First Number to allocate."
tx4$="SORT: 0 = None 1 = Alpha"
UpperLeftX = 230 : UpperLeftY = 400
WindowWidth = 300: WindowHeight = 300
statictext #b, tx1$, 40, 10, 300, 20
statictext #b, tx2$, 80, 30, 150, 20
statictext #b, tx3$, 90, 100, 150, 20
statictext #b, tx4$, 70, 160, 220, 20
textbox #b.tbox1, 50, 50, 200, 25
textbox #b.tbox2, 130, 120, 30, 25
textbox #b.tbox3, 130, 180, 30, 25
button #b.default, "Accept", [pass], UL, 120, 230, 42, 25
open "Accept or press Enter" for dialog as #b
print #b, "trapclose [quit]": bx=1
print #b.tbox2, 1
print #b.tbox3, 0
print #b.tbox1, "!setfocus"
[npass] playwave "chimes" : max = 0
mainH=hWnd(#b) : gosub [boxhold]
wait
[pass]
print #b.tbox1, "!contents? A$" : newf$ = A$
print #b.tbox2, "!contents? B$" : first = val(B$)
print #b.tbox3, "!contents? C$" : type = val(C$)-1
if type < -1 or type > 2 or A$="" then
notice "INVALID ENTRY!"
goto [npass]
end if
[bfin] bx=0 : close #b
[dirs]
tf$="Select Sample file or Click Cancel to Abort"
filedialog tf$, "*.*", File$
if File$ = "" then [quit]
#r "!font arial 10 bold"
File$ = upper$(File$)
ext$ = "*." + right$(File$,3) 'select by .EXT
sFile$ = noPath$(File$)
plen = len(File$)-len(sFile$)
path$ = left$(File$,plen)
files path$, ext$, dir$()
qtyFiles = val(dir$(0, 0))
renm = 0
[go]
#r "!cls"
for x = 1 to qtyFiles
dir$(x, 1) = right$(" " + dir$(x, 1), 9)
next x
if type < 0 then [nosort]
sort dir$(), 1, qtyFiles, 0
[nosort]
count = first : max = qtyFiles + count
row = 1 : col = 2
lm=len(str$(max))
count = first : row = 1 : col = 2
for n = first to qtyFiles
oldf$ = path$ + dir$(n, 0)
tx$=str$(n)
for check = 1 to lm
if len(tx$)"" then renf$ = fresh$
if renm = 1 then name oldf$ as renf$
o$ = n; " = name ";oldf$;" as ";renf$
print #r.tb, o$ : gosub [tbox]
row = row + 1 : count = count + 1
next n
list$ = " Use CONTINUE then EFFECT OR ABORT RENAME or PRINTOUT"
renm$ = " ** ALL ABOVE NOW RENAMED **"
tx$ = "Total Selected ";qtyFiles
if renm = 0 then tx$=tx$+list$ else tx$=tx$+renm$
tx$ = tx$ + chr$(13)+" A PRINTOUT could prove usefull for a record of the changes."
print #r.tb, tx$
row = row + 1 : col = 2 : gosub [tbox]
#r "!lines" : input #r, lpos
beep : wait
[rename]
if max = 0 then notice "NO FILES SELECTED!" : goto [pick]
renm = 1 : goto [go]
[sdump]
printerdialog
lpos = lpos - 4
for row=1 to lpos
for col=1 to 60
#r "!select ";col;" ";row;" ";
#r "!setfocus";
#r "!line ";row;" txt$";
char$=mid$(txt$,col,1)
lprint char$;
next col
lprint
next row
dump : notice "Data passed to printer"
wait
[tbox]
print #r.tb, "!selectall";
print #r.tb, "!cut";
#r "!select ";col;" ";row;
#r "!paste";
return
[boxhold]
open "user32" for dll as #user ' prevents losing box
toTop=(-1 or 0)
flags=_SWP_NOMOVE or _SWP_NOSIZE
calldll #user,"SetWindowPos",mainH as ushort,toTop as short,_
0 as short,0 as short,0 as short,0 as short,flags as ushort,_
result as void
close #user : return
[quit]
if bx = 1 then close #b
close #r : end
function noPath$(t$)
while instr(t$, "\")
t$ = mid$(t$, 2)
wend
noPath$ = t$
end function